﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBaoHanh.Template;
using BussinessLayer;
using QLBaoHanh.DoiBHSuaChua;
using DataAccessLayer;

namespace QLBaoHanh.DoiBHSuaChua
{
    public partial class frmDsThietBiDoiBHSC : Form
    {
        private IEnumerable<View_ThietBiBHDetail> _dsThietBiBHSC;
        public IEnumerable<View_ThietBiBHDetail> DsThietBiBHSC
        {
            get { return _dsThietBiBHSC; }
            set 
            { 
                _dsThietBiBHSC = value;
                //reset binding
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = _dsThietBiBHSC;
            }
        }


        public frmDsThietBiDoiBHSC()
        {
            InitializeComponent();
        }

        #region Methods

        /// <summary>
        /// Refesh form (grid)
        /// </summary>
        public void RefreshForm()
        {
            LoadDataToGrid();
        }

        /// <summary>
        /// Get selected item of grid view
        /// </summary>
        /// <returns>0 - not selected else return id</returns>
        private int getSelectedId()
        {
            int Id = 0 ;
            if (dataGridView1.SelectedRows.Count != 0)
            {
                Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            }
            return Id;
        }

        /// <summary>
        /// Load data to grid
        /// </summary>
        private void LoadDataToGrid()
        {
            int CurrentRowIndex = 0;
            if (dataGridView1.CurrentRow != null)
            {
                CurrentRowIndex = dataGridView1.CurrentRow.Index;
            }
            //getData with current filter
            GetDataWithCurrentFilter();
            //if current row index !=0 then selected it
            if (CurrentRowIndex != 0 && CurrentRowIndex < dataGridView1.Rows.Count )
            {
                this.dataGridView1.Rows[CurrentRowIndex].Selected = true;
            }
            //else Selected to first items.
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count == 0)
            {
                this.dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            }
            //LoadThietBiBaoHanh(getSelectedId());
        }
      
        public void GetDataWithCurrentFilter()
        {
            //Load het
            NghiepVuBO objNghiepVuBO = new NghiepVuBO();
            DsThietBiBHSC = objNghiepVuBO.GetDsThietBiBHSC();
        }

        private List<View_ThietBiBHDetail> getDataSelectFromGrid()
        {
            List<View_ThietBiBHDetail> dataList = new List<View_ThietBiBHDetail>();
            View_ThietBiBHDetail obj;
            int Phien_ThietBiId = 0;
            foreach (DataGridViewRow msgRow in dataGridView1.Rows)
            {
                if (msgRow.Cells[chkSelected.Name].Value !=null)
                {
                    if ((bool)msgRow.Cells[chkSelected.Name].Value == true)
                    {
                        Phien_ThietBiId = (int)msgRow.Cells["Id"].Value;
                        obj = _dsThietBiBHSC.Where<View_ThietBiBHDetail>(o => o.Id == Phien_ThietBiId).SingleOrDefault();
                        if (obj != null)
                        {
                            dataList.Add(obj);
                        }
                    }
                }
            }
            return dataList;
        }

#endregion

        #region Events

        private void frmDsThietBiDoiBHSC_Load(object sender, EventArgs e)
        {
            LoadDataToGrid();   
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int rowSelected = e.RowIndex;
                if (e.RowIndex != -1)
                {
                    this.dataGridView1.Rows[rowSelected].Selected = true;
                }
            }
        }
        
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            // nothing is selected
            if (dataGridView1.SelectedRows.Count == 0)
                return;

            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is KhachHang) && (e.ColumnIndex == 6)) //TenKhachHang
            {
                e.Value = (e.Value as KhachHang).Ten;
            }
            else if ((e.Value is KhachHang) && (e.ColumnIndex == 7)) //SDT
            {
                e.Value = (e.Value as KhachHang).Mobile;
            }
            else if (e.Value is NhanVien)
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
            else if (e.Value is TrangThaiPhien)
            {
                if ((e.Value as TrangThaiPhien).Id == (int)MyCommon.TrangThaiPhien.ThaoTacNgay_HoanThanh)
                {
                    e.Value = true;
                }
                else
                {
                    e.Value = false;
                }
                
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int PhienId = getSelectedId();
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }


        #endregion

        private void btnGuiKTV_Click(object sender, EventArgs e)
        {
            if (!HasSelectedInGrid())
            {
                MessageBox.Show("Phải chọn thiết bị cần giao", "Gửi phòng kỹ thuật", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            List<View_ThietBiBHDetail> dataList = getDataSelectFromGrid();
            frmGiaoKTV obj = new frmGiaoKTV(dataList, this);
            obj.ShowDialog();
        }

        private void btnGuiNCC_Click(object sender, EventArgs e)
        {
            if (!HasSelectedInGrid())
            {
                MessageBox.Show("Phải chọn thiết bị cần gửi nhà cung câp", "Gửi nhà cung cấp", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            List<View_ThietBiBHDetail> dataList = getDataSelectFromGrid();
            frmGuiNhaCungCao obj = new frmGuiNhaCungCao(dataList, this);
            obj.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private bool HasSelectedInGrid()
        {
            bool HasSelected = false;
            foreach (DataGridViewRow msgRow in dataGridView1.Rows)
            {
                if (msgRow.Cells[chkSelected.Name].Value != null)
                {
                    if ((bool)msgRow.Cells[chkSelected.Name].Value == true)
                    {
                        HasSelected = true;
                        break;
                    }
                }
            }
            return HasSelected;
        }

    }
}
